<template>
  <!-- 游戏报表 -->
  <div class="app-container">
    <div class="app-wrap">
      <!-- 新增人数-代理人数 -->
      <collect-details />
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh" style="height:57vh">
        <template v-slot:row>
          <el-form-item label="渠道:" label-width="50px">
            <el-select v-model="listQuery.agent_id" placeholder="请选择渠道" filterable >
              <el-option label="全部" value="" ></el-option>
              <el-option :label="'【'+item.value+'】'+item.label" :value="item.value" v-for="(item, index) in channelData" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="游戏类型:" label-width="90px">
            <el-select v-model="listQuery.game_id" placeholder="请选择游戏类型" filterable @change="chooseGameType">
              <el-option label="全部" :value="0" ></el-option>
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in GAME_TYPE" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="游戏房间:" label-width="90px">
            <el-select v-model="listQuery.room_config_id" placeholder="请选择游戏房间" filterable @change="chooseGameRoom">
              <el-option label="全部" :value="0" ></el-option>
              <div v-if="listQuery.game_id">
                <el-option v-show="listQuery.game_id==item.game_id?true:false" :label="item.game_id==1?'【'+item.a_base_room_id+'】号房间 ('+'T'+')':'【'+item.a_base_room_id+'】号房间 ('+'R'+')'" :value="item.a_base_room_id" v-for="(item, index) in roomData" :key="index"></el-option>
              </div>
              <div v-else>
                <el-option :label="item.game_id==1?'【'+item.a_base_room_id+'】号房间 ('+'T'+')':'【'+item.a_base_room_id+'】号房间 ('+'R'+')'" :value="item.a_base_room_id" v-for="(item, index) in roomData" :key="index"></el-option>
              </div>
            </el-select>
          </el-form-item>
          <el-form-item label="日期时间:" label-width="80px">
            <el-date-picker is-range v-model="listQuery.dateTime" type="date" 
            class="full-width-input time-box" :clearable="false"></el-date-picker>
          </el-form-item>
        </template>
        <template v-slot:actions>
          <div class="flexDetween">
            <div class="statistic">
              <div class="flex">
                <span> 玩家初始金额汇总：<span class="colorGre">{{ $public.mathNumberDivideWan(moneyForm.first_login_amount) }}</span></span>
                <span> 玩家剩余金额汇总：<span class="colorGre">{{ $public.mathNumberDivideWan(moneyForm.remaining_amount) }}</span></span>
              </div>
            </div>
            <!-- <el-button type="primary" :disabled="!dataList.length" size="mini" icon="el-icon-download" @click="$public.exportTable('游戏报表')">导出</el-button>  -->
          </div>
        </template>
        <template>
          <el-table-column prop="date" label="日期时间" align="center" width="150">
            <template slot-scope="scope">
              <span>{{!scope.$index && !scope.row.date?'汇总':scope.row.date?scope.row.date:'' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="agent_id" label="渠道名" align="center" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{!scope.$index && !scope.row.agent_id?'汇总':scope.row.agent_id?'【'+scope.row.agent_id+'】'+$public.channelLabel(scope.row.agent_id):'' }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="game_id" label="游戏类型" width="120">
            <template slot-scope="scope">
              <span >{{ scope.row.game_id?gameLabel(scope.row.game_id):'汇总' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="room_config_id" label="房间号" align="center" width="150">
            <template slot-scope="scope">
              <span>{{!scope.$index && !scope.row.room_config_id?'汇总':scope.row.room_config_id?scope.row.room_config_id+'号房间':'' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="today_total_game" label="总局数" align="center" width="150"/>
          <el-table-column prop="today_total_bet" label="总下注" align="center" width="180"/>
          <el-table-column prop="total_income" label="系统总输赢" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.total_income>0?'colorGre':'colorRed'">{{ scope.row.total_income }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="today_total_income" label="玩家总输赢" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.today_total_income>0?'colorGre':'colorRed'">{{ scope.row.today_total_income }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="today_total_tax" label="抽水" align="center" width="180"/>
          <el-table-column prop="today_win_money" label="赢钱玩家总额" align="center" width="180">
            <template slot-scope="scope">
              <span class="colorGre">{{ scope.row.today_win_money }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="today_win_num" label="赢钱玩家人数" align="center" width="150"/>
          <el-table-column prop="today_lose_money" label="输钱玩家总额" align="center" width="180">
            <template slot-scope="scope">
              <span class="colorRed">{{ scope.row.today_lose_money }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="today_lose_num" label="输钱玩家人数" align="center" width="150"/>
          <!-- <el-table-column prop="before_money" label="初始资金" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.before_money>0?'colorGre':'colorRed'">{{ scope.row.before_money }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="after_money" label="剩余资金" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.after_money>0?'colorGre':'colorRed'">{{ scope.row.after_money }}</span>
            </template>
          </el-table-column> -->
          <el-table-column prop="new_user_today_win_money" label="新手赢钱玩家总额" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.new_user_today_win_money>0?'colorGre':'colorRed'">{{ scope.row.new_user_today_win_money }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="new_user_today_win_num" label="新手赢钱玩家人数" align="center" width="150"/>
          <el-table-column prop="new_user_today_lose_money" label="新手输钱玩家总额" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.new_user_today_lose_money>0?'colorGre':'colorRed'">{{ scope.row.new_user_today_lose_money }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="new_user_today_lose_num" label="新手输钱玩家人数" align="center" width="150"/>
          <el-table-column prop="new_user_today_total_income" label="新手玩家总输赢" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.new_user_today_total_income>0?'colorGre':'colorRed'">{{ scope.row.new_user_today_total_income }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="normal_user_today_win_money" label="正常赢钱玩家总额" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.normal_user_today_win_money>0?'colorGre':'colorRed'">{{ scope.row.normal_user_today_win_money }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="normal_user_today_win_num" label="正常赢钱玩家人数" align="center" width="150"/>
          <el-table-column prop="normal_user_today_lose_money" label="正常输钱玩家总额" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.normal_user_today_lose_money>0?'colorGre':'colorRed'">{{ scope.row.normal_user_today_lose_money }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="normal_user_today_lose_num" label="正常输钱玩家人数" align="center" width="150"/>
          <el-table-column prop="normal_user_today_total_income" label="正常玩家总输赢" align="center" width="180">
            <template slot-scope="scope">
              <span :class="scope.row.normal_user_today_total_income>0?'colorGre':'colorRed'">{{ scope.row.normal_user_today_total_income }}</span>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
    </div>
  </div>
</template>

<script>
import {
  throttle
} from '@/utils/index'

import {
  getGameReport,
  getGameExport,
  getGameReportRoom,
  getPlayerMoney
} from '@/api/statistics'
 import {
  GAME_TYPE
} from '@/utils/enum'
import CollectDetails from './collect.vue';
import SearchTable from '@/components/SearchTable'
export default {
  name: 'gameReport',
  components: {
    CollectDetails,
    SearchTable
  },
  data () {
    return {
      GAME_TYPE,
      channelData: this.$store.state.commonState.channelDataArray,
      listQuery: {
        page: 1,
        size: 50,
        agent_id:'',
        room_config_id:0,
        game_id:0,
        dateTime:this.$public.getDateDay(0),
        start_time:'',
        end_time:'',
      },
      listLoading: true,
      dataList: [],
      currentTotal: 0,
      roomData:[],
      moneyForm:{
        first_login_amount:0,
        remaining_amount:0,
      }
    }
  },
  created () {
    // this.getDataList();
  },
  activated(){
    this.channelData=this.$store.state.commonState.channelDataArray
    this.getDataList();
    this.getGameReportRoomList();
  },
  methods: {
    // 游戏报表
    async getDataList (query) {
      let that = this,params = JSON.parse(JSON.stringify(that.listQuery));
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = []
      that.checkedItem = null
      that.listLoading = true;
      if (params.dateTime) {
        params.start_time = that.$public.standardDate(params.dateTime).substr(0,10)+' 00:00:00'
        params.end_time = that.$public.standardDate(params.dateTime).substr(0,10)+' 23:59:59'
      }else{
        params.start_time = ''
        params.end_time = ''
      }
      that.getPlayerMoneyList()
      const response = await getGameReport(params)
      if (response.code == 1) {
        if(response.data.list){
          response.data.list.forEach(item=>{
            if(params.room_config_id){
              item.room_config_id=params.room_config_id
            }
            if(params.game_id){
              item.game_id=params.game_id
            }
            item.date=params.end_time.substring(0,10)
            item.today_total_bet=that.$public.mathNumberDivideWan(item.today_total_bet)
            item.total_income=that.$public.mathNumberDivideWan(item.today_total_income)*-1
            item.today_total_income=that.$public.mathNumberDivideWan(item.today_total_income)
            item.today_total_tax=that.$public.mathNumberDivideWan(item.today_total_tax)
            item.today_win_money=that.$public.mathNumberDivideWan(item.today_win_money)
            item.today_lose_money=that.$public.mathNumberDivideWan(item.today_lose_money)
            item.before_money=that.$public.mathNumberDivideWan(item.before_money)
            item.after_money=that.$public.mathNumberDivideWan(item.after_money)
            // 新手
            item.new_user_today_win_money=that.$public.mathNumberDivideWan(item.new_user_today_win_money)
            item.new_user_today_lose_money=that.$public.mathNumberDivideWan(item.new_user_today_lose_money)
            item.new_user_today_total_income=that.$public.mathNumberDivideWan(item.new_user_today_total_income)
            // 正常
            item.normal_user_today_win_money=that.$public.mathNumberDivideWan(item.normal_user_today_win_money)
            item.normal_user_today_lose_money=that.$public.mathNumberDivideWan(item.normal_user_today_lose_money)
            item.normal_user_today_total_income=that.$public.mathNumberDivideWan(item.normal_user_today_total_income)
          })
          that.dataList = response.data.list
          that.currentTotal = response.data.total
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false
    },
    // 游戏房间
    async getGameReportRoomList(){
      let that = this,params = JSON.parse(JSON.stringify(that.listQuery));
      that.roomData = []
      if (params.dateTime) {
        params.start_time = that.$public.standardDate(params.dateTime).substr(0,10)+' 00:00:00'
        params.end_time = that.$public.standardDate(params.dateTime).substr(0,10)+' 23:59:59'
      }else{
        params.start_time = ''
        params.end_time = ''
      }
      const response = await getGameReportRoom(params)
      if (response.code == 1) {
        if(response.data){
          that.roomData = response.data
        }
      }else{
        that.$message.error(response.msg)
      }
    },
    // 玩家资金
    async getPlayerMoneyList(){
      let that = this,params = JSON.parse(JSON.stringify(that.listQuery));
      that.moneyForm = {
        first_login_amount:0,
        remaining_amount:0,
      }
      if (params.dateTime) {
        params.start_time = that.$public.standardDate(params.dateTime).substr(0,10)+' 00:00:00'
        params.end_time = that.$public.standardDate(params.dateTime).substr(0,10)+' 23:59:59'
      }else{
        params.start_time = ''
        params.end_time = ''
      }
      const response = await getPlayerMoney(params)
      if (response.code == 1) {
        if(response.data){
          that.moneyForm=response.data
        }
      }else{
        that.$message.error(response.msg)
      }
    },
    // 游戏类型展示
    gameLabel(cellValue){
      if(cellValue){
        return (this.GAME_TYPE.find((item) => item.value === cellValue)?.label || "-");
      }else{
        return '--'
      }
    },
    // 游戏类型
    chooseGameType(val){
      let that=this;
      that.listQuery.room_config_id=0
    },
    // 游戏房间
    chooseGameRoom(val){
      let that=this;
      if(val){
        that.roomData.map(item=>{
          if(item.a_base_room_id==val){
            that.listQuery.game_id=item.game_id
          }
        })
      }else{
        that.listQuery.game_id=0
      }
    },
    // 导出
    getExport(){
      let that = this;
      getGameExport(that.listQuery).then(res => {
        if (res.code == 1) {
          that.$public.documentDownload(res.data)
        }
      }).catch((error) => {
        that.$message.error(error.msg)
      })
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
        agent_id:'',
        room_config_id:0,
        game_id:0,
        dateTime:this.$public.getDateDay(0),
        start_time:'',
        end_time:'',
      }
      this.getDataList(true)
    }, 2000),
  }
}
</script>

<style lang="scss" scoped>

</style>
